package cc.rengu.redp.system.controller;

import cc.rengu.redp.common.annotation.Log;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.domain.RedpResponse;
import cc.rengu.redp.common.utils.EasyExcelUtil;
import cc.rengu.redp.system.domain.Organization;
import cc.rengu.redp.system.domain.OrganizationVO;
import cc.rengu.redp.system.service.OrganizationService;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author System
 */
@Api(tags = "机构管理")
@RestController
@RequestMapping("/organization")
@Slf4j
public class OrganizationController extends BaseController {

    @Autowired
    private OrganizationService organizationService;

    /**
     * 查询数据列表
     *
     */
    @GetMapping
    public Map<String, Object> findPageList(OrganizationVO org, QueryRequest queryRequest) {
        return this.organizationService.findOrgTrees(org);
    }

    /**
     * 查询数据列表
     *
     */
    @GetMapping("/detail/{orgId}")
    public RedpResponse findDetail(@NotBlank(message = "{required}") @PathVariable String orgId) {
        Organization db =  this.organizationService.getById(orgId);
        return new RedpResponse().data(db);
    }

    @Log("新增机构")
    @PostMapping
    @RequiresPermissions("org:add")
    public RedpResponse add(@Valid OrganizationVO org) {
        try {
            this.organizationService.add(org);
            return RedpResponse.getInstance().success();
        } catch (Exception e) {
            String message = "新增机构失败：" + e.getMessage();
            log.error(message, e);
            return RedpResponse.getInstance().message(message).failure();
        }
    }

    @Log("删除机构")
    @DeleteMapping("/{orgIds}")
    @RequiresPermissions("org:delete")
    public RedpResponse delete(@NotBlank(message = "{required}") @PathVariable String orgIds) {
        try {
            String[] ids = orgIds.split(StringPool.COMMA);
            this.organizationService.delete(Arrays.stream(ids).map(i -> Long.valueOf(i)).toArray(Long[]::new));
            return RedpResponse.getInstance().success();
        } catch (Exception e) {
            String message = "删除机构失败：" + e.getMessage();
            log.error(message, e);
            return RedpResponse.getInstance().message(message).failure();
        }
    }

    @Log("修改机构")
    @PutMapping
    @RequiresPermissions("org:update")
    public RedpResponse update(@Valid OrganizationVO org) {
        try {
            this.organizationService.update(org);
            return RedpResponse.getInstance().success();
        } catch (Exception e) {
            String message = "修改机构失败：" + e.getMessage();
            log.error(message, e);
            return RedpResponse.getInstance().message(message).failure();
        }
    }

    @PostMapping("excel")
    @RequiresPermissions("org:export")
    public void export(OrganizationVO org, HttpServletResponse response) {
        try {
            List<Organization> orgs = this.organizationService.findList(org);
            EasyExcelUtil.export(response, orgs, "机构信息");
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
        }
    }
}
